// 引用项目中已有的MVVM基础工具
open System.Windows
open Fsharp.wpf
open System
open System.Collections.ObjectModel
open System.Windows.Input
open Fsharp.wpf.Core

[<CLIMutable>]
type Person =
    {
        Name:string
        Email:string
        Age:int
    }

// 主ViewModel，直接使用项目中的基础组件
type MainViewModel() =
    inherit ViewModelBase()  // 使用项目中已有的ViewModelBase

    // 绑定属性
    let mutable name = ""
    let mutable email = ""
    let mutable age = 0
    let people = ObservableCollection<Person>()

  
    
    // 构造函数中添加示例数据
    do
        people.Add({Name = "张三"; Email = "zhangsan@example.com"; Age = 30})
        people.Add({Name = "李四"; Email = "lisi@example.com"; Age = 25})
    // 属性定义（保持不变）
    member this.Name
        with get() = name
        and set(value) =
            if name <> value then
                name <- value
                this.OnPropertyChanged()
                

    member this.Email
        with get() = email
        and set(value) =
            if email <> value then
                email <- value
                this.OnPropertyChanged()    
                

    member this.Age
        with get() = age
        and set(value) =
            if age <> value then
                age <- value
                this.OnPropertyChanged()
                

    member this.People = people

    // 命令属性
    member this.TestBindingCommand = Commands.createSimple (fun _ -> this.TestBinding())
    member this.AddCommand = Commands.createSimple (fun _ -> this.AddPerson())
    member this.RemoveCommand = Commands.createSimple this.RemovePerson

    // 命令实现（保持不变）
    member private this.TestBinding() = 
        MessageBox.Show $"当前绑定值: 姓名=%s{this.Name}, 邮箱=%s{this.Email}, 年龄=%d{this.Age}" |> ignore

    member private this.AddPerson() = 
        let newPerson = { Name = this.Name
                          Email = this.Email
                          Age = this.Age }
        people.Add(newPerson)
        // 清空输入
        this.Name <- ""
        this.Email <- ""
        this.Age <- 0
    // 函数没有成功删除列表项
    member private this.RemovePerson(param: obj) = 
        
        // 尝试使用索引删除
        match param with
        | :? Person as person -> 
            let index = people.IndexOf(person)
            if index >= 0 then
                people.RemoveAt(index)
                this.OnPropertyChanged()
            else
                // 添加调试信息，帮助诊断问题
                printfn "尝试删除不存在的人员: %A" person
        | _ -> 
            printfn "无效的参数类型: %A" param



    
// ... existing code ...

// 应用程序启动函数
module Program =

    // 应用程序入口点
    [<EntryPoint>]
    [<STAThread>]
    let main _ = 
        // 创建应用程序实例
        let app = Application()
        
        // 初始化主窗口和ViewModel
        let window = Fsharp.wpf.Xaml.fromFile<Window> "MainWindow.xaml"
        window.DataContext <- MainViewModel()
        
        // 显示主窗口并启动应用消息循环
        app.Run(window)